package com.api.gbhg.luoao.utils;

import com.api.gbhg.liuwenbo.dao.DataBaseUtil;
import com.api.gbhg.liuwenbo.pojo.Info;
import com.api.gbhg.liuwenbo.utils.AlgorithmUtil;
import weaver.conn.RecordSet;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class Upperpart {


    private Info[] paIf;

    private String nd ;

    private String dwId ;

    public void setDw(Map<String, Object> param){
        if(param.containsKey("dw") && param.get("dw") instanceof Integer ){
            this.dwId = AlgorithmUtil.getSubCompanIdBybmId((Integer) param.get("dw"));
        }else{
            this.dwId = "2"; //默认为2(拱北海关)
        }
    }

    public Upperpart(Map<String, Object> param) {

            //实体类
            paIf = new Info[4];
            //年份
            setNd(param);
            //单位
            setDw(param);

    }

    public Info[] getPaIf() {
        return paIf;
    }


    public void setNd(Map<String, Object> param) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        if(param.containsKey("nd") && param.get("nd") instanceof Integer ) {
            this.nd = param.get("nd").toString();
        }else {
            Date date = new Date();
            this.nd = sdf.format(date.getTime());
        }
    }

    public int getNd(Map<String,Object> param){
        if(param.containsKey("nd") && param.get("nd") instanceof Integer )
            return  (Integer) param.get("nd");
        else
            return LocalDate.now().getYear();
    }


    /**
     * 给预算执行率赋值
     * @return
     */
    public void setImplementRate(){
        RecordSet rs = PortalUtils.getTotalImplement(nd,dwId);
        String RealTime = "0.00";
        String YearAvg = "0.00";
        while(rs.next()){
            RealTime = AlgorithmUtil.getRealTimeImplementRate(rs.getDouble("syed"), rs.getDouble("ze"));
            YearAvg = AlgorithmUtil.getYearAvgImplRate(Integer.valueOf(nd),rs.getDouble("ze"));
        }
        Map<String,Object> map[] = new HashMap[3];
        paIf[0] = new Info();
        map[0] = new HashMap<>();
        map[0].put("content","总执行率");
        map[0].put("zxl1",RealTime);
        map[0].put("zxl2",YearAvg);
        map[0].put("state","down");

        map[1] = new HashMap<>();
        map[1].put("content","当年执行率");
        map[1].put("zxl1",RealTime);
        map[1].put("zxl2",YearAvg);
        map[1].put("state","up");

        map[2] = new HashMap<>();
        map[2].put("content","上年结转执行率");
        map[2].put("zxl1",RealTime);
        paIf[0].setTitle("预算执行率");
        paIf[0].setData(map);

    }


    /**
     * 给中央财政拨款赋值
     * @return
     */
    public void setCenterAppia(){
        RecordSet rs = PortalUtils.getAppropriationData(nd,dwId,"1");
        String RealTime = "0.00";
        String YearAvg = "0.00";
        while(rs.next()){
            RealTime = AlgorithmUtil.getRealTimeImplementRate(rs.getDouble("syed"), rs.getDouble("je"));
            YearAvg = AlgorithmUtil.getYearAvgImplRate(Integer.valueOf(nd),rs.getDouble("je"));
        }
        Map<String,Object> map[] = new HashMap[3];
        paIf[1] = new Info();
        map[0] = new HashMap<>();
        map[0].put("content","总执行率");
        map[0].put("zxl1",RealTime);
        map[0].put("zxl2",YearAvg);
        map[0].put("state","down");

        map[1] = new HashMap<>();
        map[1].put("content","当年执行率");
        map[1].put("zxl1",RealTime);
        map[1].put("zxl2",YearAvg);
        map[1].put("state","up");

        map[2] = new HashMap<>();
        map[2].put("content","上年结转执行率");
        map[2].put("zxl1","80");
        map[2].put("zxl2","60");
        map[2].put("state","down");
        paIf[1].setData(map);
        paIf[1].setTitle("中央财政拨款");
    }


    /**
     * 给地方财政拨款赋值
     * @return
     */
    public void setlocalAppia(){
        RecordSet rs = PortalUtils.getAppropriationData(nd,dwId,"3");
        String RealTime = "0.00";
        String YearAvg = "0.00";
        while(rs.next()){
            RealTime = AlgorithmUtil.getRealTimeImplementRate(rs.getDouble("syed"), rs.getDouble("je"));
            YearAvg = AlgorithmUtil.getYearAvgImplRate(Integer.valueOf(nd),rs.getDouble("je"));
        }
        Map<String,Object> map[] = new HashMap[3];
        paIf[2] = new Info();
        map[0] = new HashMap<>();
        map[0].put("content","总执行率");
        map[0].put("zxl1",RealTime);
        map[0].put("zxl2",YearAvg);
        map[0].put("state","up");

        map[1] = new HashMap<>();
        map[1].put("content","当年执行率");
        map[1].put("zxl1",RealTime);
        map[1].put("zxl2",YearAvg);
        map[1].put("state","up");

        map[2] = new HashMap<>();
        map[2].put("content","上年结转执行率");
        map[2].put("zxl1","70");
        map[2].put("zxl2","79");
        map[2].put("state","up");
        paIf[2].setData(map);
        paIf[2].setTitle("地方财政拨款");
    }


    /**
     * 给中央转移支付赋值
     * @return
     */
    public void setCenterTransfer(){
        RecordSet rs = PortalUtils.getAppropriationData(nd,dwId,"2");
        String RealTime = "0.00";
        String YearAvg = "0.00";
        while(rs.next()){
            RealTime = AlgorithmUtil.getRealTimeImplementRate(rs.getDouble("syed"), rs.getDouble("je"));
            YearAvg = AlgorithmUtil.getYearAvgImplRate(Integer.valueOf(nd),rs.getDouble("je"));
        }
        Map<String,Object> map[] = new HashMap[3];
        paIf[3] = new Info();
        map[0] = new HashMap<>();
        map[0].put("content","总执行率");
        map[0].put("zxl1",RealTime);
        map[0].put("zxl2",YearAvg);
        map[0].put("state","up");

        map[1] = new HashMap<>();
        map[1].put("content","当年执行率");
        map[1].put("zxl1",RealTime);
        map[1].put("zxl2",YearAvg);
        map[1].put("state","up");

        map[2] = new HashMap<>();
        map[2].put("content","上年结转执行率");
        map[2].put("zxl1","70");
        map[2].put("zxl2","79");
        map[2].put("state","down");
        paIf[3].setData(map);
        paIf[3].setTitle("中央转移支付");
    }




}
